package airport.user.action;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import airport.user.dao.UserDAO;
import airport.user.pojo.User;

import com.opensymphony.xwork2.ActionSupport;

public class ChangeMailAction extends ActionSupport implements
		ServletResponseAware, ServletRequestAware {

	/**
* 
*/
	private static final long serialVersionUID = 1L;
	private String password;
	private String newEmail;
	private String newEmailConfirm;
	private String username;

	protected HttpServletResponse servletResponse;

	@Override
	public void setServletResponse(HttpServletResponse servletResponse) {
		this.servletResponse = servletResponse;
	}

	protected HttpServletRequest servletRequest;

	@Override
	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;
	}

	public String execute() {

		String ret = "success";
		UserDAO dao = new UserDAO();
		dao.updateEmail(username, newEmail);
		return ret;

	}

	public void validate() {

		Cookie[] cookies = servletRequest.getCookies();
		String cookieName = "AirportUser";
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				Cookie cookie = cookies[i];
				if (cookieName.equals(cookie.getName())) {
					username = cookie.getValue();
					break;
				}
			}
		}
		UserDAO dao = new UserDAO();
		User user = dao.findUserByUserName(username);
		String pw = user.getPassword();
		
		//validate inputs are not null
		if (password == null) {
			addFieldError("password", "please enter password");
		} else if (newEmail == null) {
			addFieldError("newEmail", "please enter new email address");
		} else if (newEmailConfirm == null) {
			addFieldError("newEmailConfirm",
					"please enter new email confirmation");
		}

		//validate password is correct and new/confirmation emails match
		if (!(password.equals(pw))) {
			addFieldError("password", "invalid password");
		}
		if (!(newEmail.equals(newEmailConfirm))) {
			addFieldError("newEmailConfirm",
					"confirmation email does not match new email");
		}

		// validate that email is in correct format
		String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
		Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(newEmail);
		if (!matcher.matches()) {
			addFieldError("newEmail", "please enter a valid email address");
		}
		
		//make sure email does not already exist in database
		if(dao.findUserByEmail(newEmail) != null) {
			addFieldError("newEmail", "This email is already in use");
		}
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNewEmail() {
		return newEmail;
	}

	public void setNewEmail(String newEmail) {
		this.newEmail = newEmail;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getNewEmailConfirm() {
		return newEmailConfirm;
	}

	public void setNewEmailConfirm(String newEmailConfirm) {
		this.newEmailConfirm = newEmailConfirm;
	}

}
